﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using CsGL.OpenGL;

namespace TEST3D
{
    class OpenGLBase:OpenGLControl
    {
        const int n = 90;
        const float R = 1.0f;
        const float Pi = 3.1415926536f;

        public OpenGLBase()
        {
      
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            Size s = Size;
            // 计算窗口的纵横比
            double aspect_ratio = (double)s.Width / (double)s.Height;
            GL.glMatrixMode(GL.GL_PROJECTION);     //  设置当前为投影矩阵
            GL.glLoadIdentity();    // 重置投影矩阵
            GL.gluPerspective(45.0f, aspect_ratio, 0.1f, 100.0f);
            GL.glMatrixMode(GL.GL_MODELVIEW); // 设置当前为模型视图矩阵
            GL.glLoadIdentity();    // 重置模型视图矩阵
        }

        protected override void InitGLContext()
        {
            base.InitGLContext();

            GL.glShadeModel(GL.GL_SMOOTH);            // Enable Smooth Shading
            GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);             // Black Background
            GL.glClearDepth(2.0f);                                    //  Depth Buffer Setup
            GL.glEnable(GL.GL_DEPTH_TEST);            // Enables Depth Testing
            GL.glDepthFunc(GL.GL_LEQUAL);              //  The Type Of Depth Testing To Do
            GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

            // TODO: 在此添加其他初始化动作，比如建立显示列表
        }

        protected override void OnCreateControl()
        {
            GL.glClearDepth(1.0f);
            GL.glDepthFunc(GL.GL_LEQUAL);
            GL.glEnable(GL.GL_DEPTH_TEST);
        }

        public override void glDraw()
        {
            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // 清理视窗以及深度缓存
            GL.glLoadIdentity(); // 重置当前模型视图矩阵
            GL.gluLookAt(0.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    //设置视点

            GL.glBegin(GL.GL_LINES);
            //X轴
            GL.glVertex3f(0.0f, 0.0f, 0.0f);
            GL.glVertex3f(1.5f, 0.0f, 0.0f);
            //Y轴
            GL.glVertex3f(0.0f, 0.0f, 0.0f);
            GL.glVertex3f(0.0f, 1.5f, 0.0f);
            //Z轴
            GL.glVertex3f(0.0f, 0.0f, 0.0f);
            GL.glVertex3f(0.0f, 0.0f, 1.5f);
            GL.glEnd();

            GL.glEnable(GL.GL_LINE_STIPPLE);
            GL.glLineStipple(2, 0xAAAA);
            GL.glBegin(GL.GL_LINES);
            GL.glVertex3f(0.0f, 0.0f, 0.0f);
            GL.glVertex3f(-1.5f, 0.0f, 0.0f);

            GL.glVertex3f(0.0f, 0.0f, 0.0f);
            GL.glVertex3f(0.0f, -1.5f, 0.0f);

            GL.glVertex3f(0.0f, 0.0f, 0.0f);
            GL.glVertex3f(0.0f, 0.0f, -1.5f);
            GL.glEnd();
            GL.glDisable(GL.GL_LINE_STIPPLE);



            float[] mat_ambient = { 0.8f, 0.8f, 0.8f, 1.0f };
            float[] mat_diffuse = { 0.8f, 0.0f, 0.8f, 1.0f };
            float[] mat_specular = { 1.0f, 0.0f, 1.0f, 1.0f };
            float mat_shininess = 50.0f;
            GL.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT,mat_ambient);
            GL.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_diffuse);
            GL.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular);
            GL.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess);

            float[] light_position = { 1.0f, 1.0f, 25.0f, 0.0f };
//             float[] light_diffuse = { 0.0f, 0.0f, 1.0f, 1.0f };
            GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
//             GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_diffuse);
            GL.glEnable(GL.GL_LIGHT0);
            GL.glEnable(GL.GL_LIGHTING);
            GL.glDepthFunc(GL.GL_LESS);
            GL.glEnable(GL.GL_DEPTH_TEST);



            GL.glBegin(GL.GL_QUAD_STRIP);
            GL.glVertex3f(-1.0f, -0.1f, 1.0f);
            GL.glVertex3f(-1.0f, 0.1f, 1.0f);
            GL.glVertex3f(1.0f, -0.1f, 1.0f);
            GL.glVertex3f(1.0f, 0.1f, 1.0f);

            GL.glVertex3f(1.0f, -0.1f, -1.0f);
            GL.glVertex3f(1.0f, 0.1f, -1.0f);

            GL.glVertex3f(-1.0f, -0.1f, -1.0f);
            GL.glVertex3f(-1.0f, 0.1f, -1.0f);

            GL.glVertex3f(-1.0f, -0.1f, 1.0f);
            GL.glVertex3f(-1.0f, 0.1f, 1.0f);
            GL.glEnd();

            GL.glBegin(GL.GL_QUADS);
            GL.glVertex3f(-1.0f, 0.1f, 1.0f);
            GL.glVertex3f(-1.0f, 0.1f, -1.0f);
            GL.glVertex3f(1.0f, 0.1f, -1.0f);
            GL.glVertex3f(1.0f, 0.1f, 1.0f);

            GL.glVertex3f(-1.0f, -0.1f, 1.0f);
            GL.glVertex3f(-1.0f, -0.1f, -1.0f);
            GL.glVertex3f(1.0f, -0.1f, -1.0f);
            GL.glVertex3f(1.0f, -0.1f, 1.0f);

            GL.glEnd();

            GL.glFlush();
        }
    }
    
}
